package com.gobuy.canal.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Gaozexi on 2020/6/7 15:17
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Configuration
public class RabbitMQConfig {

    //定义队列名称
    public static final String AD_UPDATE_QUEUE="ad_update_queue";

    //定义接收上下架消息的队列
    public static final String IS_MARKETABLE_QUEUE="is_marketable_queue";

    //定义接收 生成页面消息的 队列
    public static final String PAGE_CREATE_QUEUE="page_create_queue";

    //定义上下架消息的交换机
    public static final String GOODS_UP_EXCHANGE="goods_up_exchange";




    //声明队列  接收广告更新消息
    @Bean
    public Queue queue(){
        return new Queue(AD_UPDATE_QUEUE);
    }

    //声明队列 接收上下架消息的QUEUE
    @Bean("is_marketable_queue")
    public Queue IS_MARKETABLE_QUEUE(){
        return new Queue(IS_MARKETABLE_QUEUE);
    }

    //声明队列  接收 生成页面消息的 队列
    @Bean("page_create_queue")
    public Queue PAGE_CREATE_QUEUE(){
        return new Queue(PAGE_CREATE_QUEUE);
    }


    //声明交换机  转发spu表变化的消息交换机
    @Bean("goods_up_exchange")
    public Exchange GOODS_UP_EXCHANGE(){
        //生成一个 name=IS_MARKETABLE_QUEUE的,可序列化的  广播模式的交换机
        return ExchangeBuilder.fanoutExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }

    //声明绑定关系
    @Bean
    public Binding IS_MARKETABLE_QUEUE_BINDING(@Qualifier(IS_MARKETABLE_QUEUE) Queue queue,
                                               @Qualifier (GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding PAGE_CREATE_QUEUE_BINDING(@Qualifier(PAGE_CREATE_QUEUE) Queue queue,
                                               @Qualifier (GOODS_UP_EXCHANGE) Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

}
